package com.sunky.db.util;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.sunky.db.greendao.DaoMaster;
import com.sunky.db.greendao.DaoSession;

/**
 * 数据库管理类
 */
public class DBManager {

    private static DBManager sDBManager;

    public static DBManager getInstance(){
        if (sDBManager == null){
            synchronized (DBManager.class){
                if (sDBManager == null){
                    sDBManager = new DBManager();
                }
            }
        }
        return sDBManager;
    }

//    private Context mContext;
    private DaoMaster.DevOpenHelper mHelper;
    private SQLiteDatabase db;
    private DaoMaster mDaoMaster;
    private DaoSession mDaoSession;

    private DBManager(){}

    /**
     * 初始化工作,在application中调用
     * @param context
     */
    public void init(Context context){
//        mContext = context;
        mHelper = new DaoMaster.DevOpenHelper(context, "rfid-db", null);
        db = mHelper.getWritableDatabase();
        mDaoMaster = new DaoMaster(db);
        mDaoSession = mDaoMaster.newSession();
    }

    public DaoSession getDaoSession() {
        return mDaoSession;
    }
    public SQLiteDatabase getDb() {
        return db;
    }
}
